using System;
using System.Collections.Generic;
using System.Linq;

namespace MovieDB
{
    /// <summary>
    /// 电影数据库控制台应用程序
    /// 提供用户交互界面，用于管理电影数据库
    /// </summary>
    class Program
    {
        private static MovieDBManager _dbManager;
        
        /// <summary>
        /// 程序入口
        /// </summary>
        /// <param name="args">命令行参数</param>
        static void Main(string[] args)
        {
            Console.WriteLine("欢迎使用电影数据库管理系统！");
            
            // 初始化数据库连接
            InitializeDatabase();
            
            try
            {
                // 显示主菜单
                ShowMainMenu();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"程序出现错误: {ex.Message}");
            }
            finally
            {
                // 释放数据库资源
                _dbManager?.Dispose();
                Console.WriteLine("\n感谢使用，再见！");
            }
        }
        
        /// <summary>
        /// 初始化数据库连接
        /// </summary>
        private static void InitializeDatabase()
        {
            bool validConnection = false;
            
            while (!validConnection)
            {
                try
                {
                    Console.WriteLine("请选择数据库类型：");
                    Console.WriteLine("1. MySQL");
                    Console.WriteLine("2. SQL Server");
                    
                    string choice = Console.ReadLine();
                    bool useMySql = choice == "1";
                    
                    string connectionString;
                    
                    if (useMySql)
                    {
                        // MySQL连接设置
                        Console.WriteLine("请输入MySQL连接信息：");
                        Console.Write("服务器(默认localhost): ");
                        string server = Console.ReadLine();
                        if (string.IsNullOrWhiteSpace(server)) server = "localhost";
                        
                        Console.Write("端口(默认3306): ");
                        string portStr = Console.ReadLine();
                        int port = 3306;
                        if (!string.IsNullOrWhiteSpace(portStr))
                        {
                            if (!int.TryParse(portStr, out port))
                            {
                                Console.WriteLine("端口无效，使用默认值3306");
                                port = 3306;
                            }
                        }
                        
                        Console.Write("数据库名(默认MovieDB): ");
                        string database = Console.ReadLine();
                        if (string.IsNullOrWhiteSpace(database)) database = "MovieDB";
                        
                        Console.Write("用户名(默认root): ");
                        string userId = Console.ReadLine();
                        if (string.IsNullOrWhiteSpace(userId)) userId = "root";
                        
                        Console.Write("密码: ");
                        string password = ReadPassword();
                        
                        connectionString = MovieDBManager.GetMySqlConnectionString(server, database, userId, password, port);
                    }
                    else
                    {
                        // SQL Server连接设置
                        Console.WriteLine("请输入SQL Server连接信息：");
                        Console.Write("服务器(默认localhost): ");
                        string server = Console.ReadLine();
                        if (string.IsNullOrWhiteSpace(server)) server = "localhost";
                        
                        Console.Write("数据库名(默认MovieDB): ");
                        string database = Console.ReadLine();
                        if (string.IsNullOrWhiteSpace(database)) database = "MovieDB";
                        
                        Console.Write("是否使用Windows身份验证 (Y/N，默认Y): ");
                        string authChoice = Console.ReadLine();
                        bool integratedSecurity = string.IsNullOrWhiteSpace(authChoice) || authChoice.ToUpper() == "Y";
                        
                        string userId = "";
                        string password = "";
                        
                        if (!integratedSecurity)
                        {
                            Console.Write("用户名: ");
                            userId = Console.ReadLine();
                            
                            Console.Write("密码: ");
                            password = ReadPassword();
                        }
                        
                        connectionString = MovieDBManager.GetSqlServerConnectionString(server, database, integratedSecurity, userId, password);
                    }
                    
                    // 创建数据库管理器
                    _dbManager = new MovieDBManager(connectionString, useMySql);
                    _dbManager.OpenConnection();
                    validConnection = true;
                    Console.WriteLine("数据库连接成功！\n");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"数据库连接失败: {ex.Message}");
                    Console.WriteLine("请重新输入连接信息。\n");
                }
            }
        }
        
        /// <summary>
        /// 读取密码（隐藏输入）
        /// </summary>
        /// <returns>输入的密码</returns>
        private static string ReadPassword()
        {
            string password = "";
            ConsoleKeyInfo key;
            
            do
            {
                key = Console.ReadKey(true);
                
                // 忽略退格键
                if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
                {
                    password += key.KeyChar;
                    Console.Write("*");
                }
                else if (key.Key == ConsoleKey.Backspace && password.Length > 0)
                {
                    password = password.Substring(0, password.Length - 1);
                    Console.Write("\b \b");
                }
            }
            while (key.Key != ConsoleKey.Enter);
            
            Console.WriteLine();
            return password;
        }
        
        /// <summary>
        /// 显示主菜单
        /// </summary>
        private static void ShowMainMenu()
        {
            bool exit = false;
            
            while (!exit)
            {
                Console.WriteLine("========= 电影数据库管理系统 ========");
                Console.WriteLine("1. 艺人管理");
                Console.WriteLine("2. 电影管理");
                Console.WriteLine("3. 参与关系管理");
                Console.WriteLine("4. 数据查询");
                Console.WriteLine(0 + ". 退出系统");
                Console.WriteLine("======================================");
                Console.Write("请选择操作: ");
                
                string choice = Console.ReadLine();
                Console.WriteLine();
                
                switch (choice)
                {
                    case "1":
                        ShowArtistMenu();
                        break;
                    case "2":
                        ShowMovieMenu();
                        break;
                    case "3":
                        ShowParticipationMenu();
                        break;
                    case "4":
                        ShowQueryMenu();
                        break;
                    case "0":
                        exit = true;
                        break;
                    default:
                        Console.WriteLine("无效的选择，请重新输入。\n");
                        break;
                }
            }
        }
        
        /// <summary>
        /// 显示艺人管理菜单
        /// </summary>
        private static void ShowArtistMenu()
        {
            bool back = false;
            
            while (!back)
            {
                Console.WriteLine("========= 艺人管理 ========");
                Console.WriteLine("1. 添加艺人");
                Console.WriteLine("2. 查看所有艺人");
                Console.WriteLine("3. 根据ID查找艺人");
                Console.WriteLine("4. 修改艺人信息");
                Console.WriteLine("5. 删除艺人");
                Console.WriteLine(0 + ". 返回主菜单");
                Console.WriteLine("==========================");
                Console.Write("请选择操作: ");
                
                string choice = Console.ReadLine();
                Console.WriteLine();
                
                switch (choice)
                {
                    case "1":
                        AddArtist();
                        break;
                    case "2":
                        ViewAllArtists();
                        break;
                    case "3":
                        FindArtistById();
                        break;
                    case "4":
                        UpdateArtist();
                        break;
                    case "5":
                        DeleteArtist();
                        break;
                    case "0":
                        back = true;
                        break;
                    default:
                        Console.WriteLine("无效的选择，请重新输入。\n");
                        break;
                }
            }
        }
        
        /// <summary>
        /// 添加艺人
        /// </summary>
        private static void AddArtist()
        {    
            try
            {    
                Artist artist = new Artist();
                
                Console.Write("请输入艺人姓名: ");
                artist.Name = Console.ReadLine();
                
                Console.Write("请输入生日 (yyyy-MM-dd，留空跳过): ");
                string birthdayStr = Console.ReadLine();
                if (!string.IsNullOrWhiteSpace(birthdayStr))
                {    
                    if (DateTime.TryParse(birthdayStr, out DateTime birthday))
                    {    
                        artist.Birthday = birthday;
                    }
                    else
                    {    
                        Console.WriteLine("生日格式无效，跳过设置。");
                    }
                }
                
                Console.Write("请输入性别 (M/F): ");
                string sexStr = Console.ReadLine();
                if (!string.IsNullOrWhiteSpace(sexStr))
                {    
                    artist.Sex = char.ToUpper(sexStr[0]);
                }
                else
                {    
                    artist.Sex = 'U'; // 默认未知
                }
                
                Console.Write("请输入IMDB编号: ");
                artist.ImdbNumber = Console.ReadLine();
                
                Console.Write("请输入简介: ");
                artist.Introduction = Console.ReadLine();
                
                int result = _dbManager.AddArtist(artist);
                Console.WriteLine($"成功添加了{result}个艺人！艺人ID: {artist.Id}\n");
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"添加艺人失败: {ex.Message}\n");
            }
        }
        
        /// <summary>
        /// 查看所有艺人
        /// </summary>
        private static void ViewAllArtists()
        {    
            try
            {    
                List<Artist> artists = _dbManager.GetAllArtists();
                
                if (artists.Count == 0)
                {    
                    Console.WriteLine("当前没有艺人信息。\n");
                    return;
                }
                
                Console.WriteLine("========= 艺人列表 ========");
                foreach (var artist in artists)
                {    
                    Console.WriteLine($"ID: {artist.Id}");
                    Console.WriteLine($"姓名: {artist.Name}");
                    Console.WriteLine($"生日: {artist.Birthday?.ToString("yyyy-MM-dd") ?? "未知"}");
                    Console.WriteLine($"性别: {artist.Sex}");
                    Console.WriteLine($"IMDB编号: {artist.ImdbNumber ?? "无"}");
                    Console.WriteLine($"简介: {artist.Introduction ?? "无"}");
                    Console.WriteLine("--------------------------");
                }
                Console.WriteLine("==========================\n");
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"查询艺人失败: {ex.Message}\n");
            }
        }
        
        /// <summary>
        /// 根据ID查找艺人
        /// </summary>
        private static void FindArtistById()
        {    
            try
            {    
                Console.Write("请输入艺人ID: ");
                if (int.TryParse(Console.ReadLine(), out int id))
                {    
                    Artist artist = _dbManager.GetArtistById(id);
                    
                    if (artist != null)
                    {    
                        Console.WriteLine("========= 艺人信息 ========");
                        Console.WriteLine($"ID: {artist.Id}");
                        Console.WriteLine($"姓名: {artist.Name}");
                        Console.WriteLine($"生日: {artist.Birthday?.ToString("yyyy-MM-dd") ?? "未知"}");
                        Console.WriteLine($"性别: {artist.Sex}");
                        Console.WriteLine($"IMDB编号: {artist.ImdbNumber ?? "无"}");
                        Console.WriteLine($"简介: {artist.Introduction ?? "无"}");
                        Console.WriteLine("==========================\n");
                    }
                    else
                    {    
                        Console.WriteLine("未找到该艺人。\n");
                    }
                }
                else
                {    
                    Console.WriteLine("无效的ID格式。\n");
                }
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"查找艺人失败: {ex.Message}\n");
            }
        }
        
        /// <summary>
        /// 修改艺人信息
        /// </summary>
        private static void UpdateArtist()
        {    
            try
            {    
                Console.Write("请输入艺人ID: ");
                if (int.TryParse(Console.ReadLine(), out int id))
                {    
                    Artist artist = _dbManager.GetArtistById(id);
                    
                    if (artist != null)
                    {    
                        Console.WriteLine($"当前姓名: {artist.Name}");
                        Console.Write("新姓名 (留空保持不变): ");
                        string newName = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(newName))
                        {    
                            artist.Name = newName;
                        }
                        
                        Console.WriteLine($"当前生日: {artist.Birthday?.ToString("yyyy-MM-dd") ?? "未知"}");
                        Console.Write("新生日 (yyyy-MM-dd，留空保持不变): ");
                        string birthdayStr = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(birthdayStr))
                        {    
                            if (DateTime.TryParse(birthdayStr, out DateTime birthday))
                            {    
                                artist.Birthday = birthday;
                            }
                            else
                            {    
                                Console.WriteLine("生日格式无效，保持不变。");
                            }
                        }
                        
                        Console.WriteLine($"当前性别: {artist.Sex}");
                        Console.Write("新性别 (M/F，留空保持不变): ");
                        string sexStr = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(sexStr))
                        {    
                            artist.Sex = char.ToUpper(sexStr[0]);
                        }
                        
                        Console.WriteLine($"当前IMDB编号: {artist.ImdbNumber ?? "无"}");
                        Console.Write("新IMDB编号 (留空保持不变): ");
                        string newImdbNumber = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(newImdbNumber))
                        {    
                            artist.ImdbNumber = newImdbNumber;
                        }
                        
                        Console.WriteLine($"当前简介: {artist.Introduction ?? "无"}");
                        Console.Write("新简介 (留空保持不变): ");
                        string newIntroduction = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(newIntroduction))
                        {    
                            artist.Introduction = newIntroduction;
                        }
                        
                        int result = _dbManager.UpdateArtist(artist);
                        if (result > 0)
                        {    
                            Console.WriteLine("艺人信息更新成功！\n");
                        }
                        else
                        {    
                            Console.WriteLine("艺人信息更新失败！\n");
                        }
                    }
                    else
                    {    
                        Console.WriteLine("未找到该艺人。\n");
                    }
                }
                else
                {    
                    Console.WriteLine("无效的ID格式。\n");
                }
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"更新艺人信息失败: {ex.Message}\n");
            }
        }
        
        /// <summary>
        /// 删除艺人
        /// </summary>
        private static void DeleteArtist()
        {    
            try
            {    
                Console.Write("请输入艺人ID: ");
                if (int.TryParse(Console.ReadLine(), out int id))
                {    
                    Artist artist = _dbManager.GetArtistById(id);
                    
                    if (artist != null)
                    {    
                        Console.WriteLine("确认删除以下艺人信息？");
                        Console.WriteLine($"姓名: {artist.Name}");
                        Console.Write("输入 Y 确认删除，其他键取消: ");
                        string confirm = Console.ReadLine();
                        
                        if (confirm.ToUpper() == "Y")
                        {    
                            int result = _dbManager.DeleteArtist(id);
                            if (result > 0)
                            {    
                                Console.WriteLine("艺人删除成功！\n");
                            }
                            else
                            {    
                                Console.WriteLine("艺人删除失败！\n");
                            }
                        }
                        else
                        {    
                            Console.WriteLine("已取消删除操作。\n");
                        }
                    }
                    else
                    {    
                        Console.WriteLine("未找到该艺人。\n");
                    }
                }
                else
                {    
                    Console.WriteLine("无效的ID格式。\n");
                }
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"删除艺人失败: {ex.Message}\n");
            }
        }
        
        /// <summary>
        /// 显示电影管理菜单
        /// </summary>
        private static void ShowMovieMenu()
        {    
            bool back = false;
            
            while (!back)
            {    
                Console.WriteLine("========= 电影管理 ========");
                Console.WriteLine("1. 添加电影");
                Console.WriteLine("2. 查看所有电影");
                Console.WriteLine("3. 根据ID查找电影");
                Console.WriteLine("4. 修改电影信息");
                Console.WriteLine(0 + ". 返回主菜单");
                Console.WriteLine("==========================");
                Console.Write("请选择操作: ");
                
                string choice = Console.ReadLine();
                Console.WriteLine();
                
                switch (choice)
                {    
                    case "1":
                        AddMovie();
                        break;
                    case "2":
                        ViewAllMovies();
                        break;
                    case "3":
                        FindMovieById();
                        break;
                    case "4":
                        UpdateMovie();
                        break;
                    case "0":
                        back = true;
                        break;
                    default:
                        Console.WriteLine("无效的选择，请重新输入。\n");
                        break;
                }
            }
        }
        
        /// <summary>
        /// 添加电影
        /// </summary>
        private static void AddMovie()
        {    
            try
            {    
                Movie movie = new Movie();
                
                Console.Write("请输入电影名称: ");
                movie.Name = Console.ReadLine();
                
                Console.Write("请输入上映日期 (yyyy-MM-dd，留空跳过): ");
                string releaseDateStr = Console.ReadLine();
                if (!string.IsNullOrWhiteSpace(releaseDateStr))
                {    
                    if (DateTime.TryParse(releaseDateStr, out DateTime releaseDate))
                    {    
                        movie.ReleaseDate = releaseDate;
                    }
                    else
                    {    
                        Console.WriteLine("日期格式无效，跳过设置。");
                    }
                }
                
                Console.Write("请输入电影时长(分钟): ");
                if (int.TryParse(Console.ReadLine(), out int duration))
                {    
                    movie.Duration = duration;
                }
                else
                {    
                    Console.WriteLine("时长无效，设置为0。");
                    movie.Duration = 0;
                }
                
                Console.Write("请输入语言: ");
                movie.Language = Console.ReadLine();
                
                Console.Write("请输入简介: ");
                movie.Introduction = Console.ReadLine();
                
                Console.Write("请输入电影评级 (0.0-10.0，留空跳过): ");
                string ratingStr = Console.ReadLine();
                if (!string.IsNullOrWhiteSpace(ratingStr))
                {
                    if (float.TryParse(ratingStr, out float rating))
                    {
                        movie.Rating = rating;
                    }
                    else
                    {
                        Console.WriteLine("评级格式无效，跳过设置。");
                    }
                }
                
                int result = _dbManager.AddMovie(movie);
                Console.WriteLine($"成功添加了{result}个电影！电影ID: {movie.Id}\n");
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"添加电影失败: {ex.Message}\n");
            }
        }
        
        /// <summary>
        /// 查看所有电影
        /// </summary>
        private static void ViewAllMovies()
        {    
            try
            {    
                List<Movie> movies = _dbManager.GetAllMovies();
                
                if (movies.Count == 0)
                {    
                    Console.WriteLine("当前没有电影信息。\n");
                    return;
                }
                
                Console.WriteLine("========= 电影列表 ========");
                foreach (var movie in movies)
                {    
                    Console.WriteLine($"ID: {movie.Id}");
                    Console.WriteLine($"名称: {movie.Name}");
                    Console.WriteLine($"上映日期: {movie.ReleaseDate?.ToString("yyyy-MM-dd") ?? "未知"}");
                    Console.WriteLine($"时长: {movie.Duration}分钟");
                    Console.WriteLine($"语言: {movie.Language ?? "未知"}");
                    Console.WriteLine($"评级: {(movie.Rating.HasValue ? movie.Rating.Value.ToString("0.0") : "未评分")}");
                    Console.WriteLine($"简介: {movie.Introduction ?? "无"}");
                    Console.WriteLine("--------------------------");
                }
                Console.WriteLine("==========================\n");
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"查询电影失败: {ex.Message}\n");
            }
        }
        
        /// <summary>
        /// 根据ID查找电影
        /// </summary>
        private static void FindMovieById()
        {    
            try
            {    
                Console.Write("请输入电影ID: ");
                if (int.TryParse(Console.ReadLine(), out int id))
                {    
                    Movie movie = _dbManager.GetMovieById(id);
                    
                    if (movie != null)
                    {    
                        Console.WriteLine("========= 电影信息 ========");
                        Console.WriteLine($"ID: {movie.Id}");
                        Console.WriteLine($"名称: {movie.Name}");
                        Console.WriteLine($"上映日期: {movie.ReleaseDate?.ToString("yyyy-MM-dd") ?? "未知"}");
                        Console.WriteLine($"时长: {movie.Duration}分钟");
                        Console.WriteLine($"语言: {movie.Language ?? "未知"}");
                    Console.WriteLine($"评级: {(movie.Rating.HasValue ? movie.Rating.Value.ToString("0.0") : "未评分")}");
                    Console.WriteLine($"简介: {movie.Introduction ?? "无"}");
                    Console.WriteLine("==========================\n");
                    }
                    else
                    {    
                        Console.WriteLine("未找到该电影。\n");
                    }
                }
                else
                {    
                    Console.WriteLine("无效的ID格式。\n");
                }
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"查找电影失败: {ex.Message}\n");
            }
        }
        
        /// <summary>
        /// 修改电影信息
        /// </summary>
        private static void UpdateMovie()
        {
            try
            {
                Console.Write("请输入电影ID: ");
                if (int.TryParse(Console.ReadLine(), out int id))
                {
                    Movie movie = _dbManager.GetMovieById(id);
                    
                    if (movie != null)
                    {
                        Console.WriteLine($"当前名称: {movie.Name}");
                        Console.Write("新名称 (留空保持不变): ");
                        string newName = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(newName))
                        {
                            movie.Name = newName;
                        }
                        
                        Console.WriteLine($"当前上映日期: {movie.ReleaseDate?.ToString("yyyy-MM-dd") ?? "未知"}");
                        Console.Write("新上映日期 (yyyy-MM-dd，留空保持不变): ");
                        string releaseDateStr = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(releaseDateStr))
                        {
                            if (DateTime.TryParse(releaseDateStr, out DateTime releaseDate))
                            {
                                movie.ReleaseDate = releaseDate;
                            }
                            else
                            {
                                Console.WriteLine("日期格式无效，保持不变。");
                            }
                        }
                        
                        Console.WriteLine($"当前时长: {movie.Duration}分钟");
                        Console.Write("新时长 (分钟，留空保持不变): ");
                        string durationStr = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(durationStr))
                        {
                            if (int.TryParse(durationStr, out int duration))
                            {
                                movie.Duration = duration;
                            }
                            else
                            {
                                Console.WriteLine("时长格式无效，保持不变。");
                            }
                        }
                        
                        Console.WriteLine($"当前语言: {movie.Language ?? "未知"}");
                        Console.Write("新语言 (留空保持不变): ");
                        string newLanguage = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(newLanguage))
                        {
                            movie.Language = newLanguage;
                        }
                        
                        Console.WriteLine($"当前评级: {(movie.Rating.HasValue ? movie.Rating.Value.ToString("0.0") : "未评分")}");
                        Console.Write("新评级 (0.0-10.0，留空保持不变): ");
                        string ratingStr = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(ratingStr))
                        {
                            if (float.TryParse(ratingStr, out float rating))
                            {
                                movie.Rating = rating;
                            }
                            else
                            {
                                Console.WriteLine("评级格式无效，保持不变。");
                            }
                        }
                        
                        Console.WriteLine($"当前简介: {movie.Introduction ?? "无"}");
                        Console.Write("新简介 (留空保持不变): ");
                        string newIntroduction = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(newIntroduction))
                        {
                            movie.Introduction = newIntroduction;
                        }
                        
                        int result = _dbManager.UpdateMovie(movie);
                        if (result > 0)
                        {
                            Console.WriteLine("电影信息更新成功！\n");
                        }
                        else
                        {
                            Console.WriteLine("电影信息更新失败！\n");
                        }
                    }
                    else
                    {
                        Console.WriteLine("未找到该电影。\n");
                    }
                }
                else
                {
                    Console.WriteLine("无效的ID格式。\n");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"更新电影信息失败: {ex.Message}\n");
            }
        }

        /// <summary>
        /// 显示参与关系管理菜单
        /// </summary>
        private static void ShowParticipationMenu()
        {    
            bool back = false;
            
            while (!back)
            {    
                Console.WriteLine("========= 参与关系管理 ========");
                Console.WriteLine("1. 添加艺人参与电影关系");
                Console.WriteLine("0. 返回主菜单");
                Console.WriteLine("==============================");
                Console.Write("请选择操作: ");
                
                string choice = Console.ReadLine();
                Console.WriteLine();
                
                switch (choice)
                {    
                    case "1":
                        AddParticipation();
                        break;
                    case "0":
                        back = true;
                        break;
                    default:
                        Console.WriteLine("无效的选择，请重新输入。\n");
                        break;
                }
            }
        }
        
        /// <summary>
        /// 添加艺人参与电影关系
        /// </summary>
        private static void AddParticipation()
        {    
            try
            {    
                Participation participation = new Participation();
                
                Console.Write("请输入艺人ID: ");
                if (int.TryParse(Console.ReadLine(), out int artistId))
                {    
                    Artist artist = _dbManager.GetArtistById(artistId);
                    if (artist == null)
                    {    
                        Console.WriteLine("未找到该艺人，请先添加艺人信息。\n");
                        return;
                    }
                    participation.ArtistId = artistId;
                }
                else
                {    
                    Console.WriteLine("无效的艺人ID。\n");
                    return;
                }
                
                Console.Write("请输入电影ID: ");
                if (int.TryParse(Console.ReadLine(), out int movieId))
                {    
                    Movie movie = _dbManager.GetMovieById(movieId);
                    if (movie == null)
                    {    
                        Console.WriteLine("未找到该电影，请先添加电影信息。\n");
                        return;
                    }
                    participation.MovieId = movieId;
                }
                else
                {    
                    Console.WriteLine("无效的电影ID。\n");
                    return;
                }
                
                Console.Write("请输入角色名称: ");
                participation.RoleName = Console.ReadLine();
                
                int result = _dbManager.AddParticipation(participation);
                Console.WriteLine($"成功添加了艺人参与电影关系！\n");
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"添加参与关系失败: {ex.Message}\n");
            }
        }
        
        /// <summary>
        /// 显示查询菜单
        /// </summary>
        private static void ShowQueryMenu()
        {    
            bool back = false;
            
            while (!back)
            {    
                Console.WriteLine("========= 数据查询 ========");
                Console.WriteLine("1. 查询电影的所有演员");
                Console.WriteLine("2. 查询艺人参演的所有电影");
                Console.WriteLine("0. 返回主菜单");
                Console.WriteLine("==========================");
                Console.Write("请选择操作: ");
                
                string choice = Console.ReadLine();
                Console.WriteLine();
                
                switch (choice)
                {    
                    case "1":
                        QueryMovieArtists();
                        break;
                    case "2":
                        QueryArtistMovies();
                        break;
                    case "0":
                        back = true;
                        break;
                    default:
                        Console.WriteLine("无效的选择，请重新输入。\n");
                        break;
                }
            }
        }
        
        /// <summary>
        /// 查询电影的所有演员
        /// </summary>
        private static void QueryMovieArtists()
        {    
            try
            {    
                Console.Write("请输入电影ID: ");
                if (int.TryParse(Console.ReadLine(), out int movieId))
                {    
                    Movie movie = _dbManager.GetMovieById(movieId);
                    if (movie == null)
                    {    
                        Console.WriteLine("未找到该电影。\n");
                        return;
                    }
                    
                    List<Artist> artists = _dbManager.GetArtistsByMovie(movieId);
                    
                    Console.WriteLine($"电影 '{movie.Name}' 的演员列表：");
                    if (artists.Count == 0)
                    {    
                        Console.WriteLine("当前没有演员信息。\n");
                    }
                    else
                    {    
                        foreach (var artist in artists)
                        {    
                            Console.WriteLine($"- {artist.Name} (ID: {artist.Id})");
                        }
                        Console.WriteLine();
                    }
                }
                else
                {    
                    Console.WriteLine("无效的电影ID。\n");
                }
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"查询失败: {ex.Message}\n");
            }
        }
        
        /// <summary>
        /// 查询艺人参演的所有电影
        /// </summary>
        private static void QueryArtistMovies()
        {    
            try
            {    
                Console.Write("请输入艺人ID: ");
                if (int.TryParse(Console.ReadLine(), out int artistId))
                {    
                    Artist artist = _dbManager.GetArtistById(artistId);
                    if (artist == null)
                    {    
                        Console.WriteLine("未找到该艺人。\n");
                        return;
                    }
                    
                    List<Movie> movies = _dbManager.GetMoviesByArtist(artistId);
                    
                    Console.WriteLine($"艺人 '{artist.Name}' 参演的电影列表：");
                    if (movies.Count == 0)
                    {    
                        Console.WriteLine("当前没有参演电影信息。\n");
                    }
                    else
                    {    
                        foreach (var movie in movies)
                        {    
                            Console.WriteLine($"- {movie.Name} (ID: {movie.Id}) - 上映日期: {movie.ReleaseDate?.ToString("yyyy-MM-dd") ?? "未知"}");
                        }
                        Console.WriteLine();
                    }
                }
                else
                {    
                    Console.WriteLine("无效的艺人ID。\n");
                }
            }
            catch (Exception ex)
            {    
                Console.WriteLine($"查询失败: {ex.Message}\n");
            }
        }
    }
}